package fun.trackcam.game.tennis;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import fun.trackcam.game.balls.Ball;
import fun.trackcam.game.balls.BallsGame;

/**
 * A simple tennis Ball Game
 * 
 * @author Admin
 * 
 */
public class TennisGame extends BallsGame {
	/**
	 * how many balls?
	 */
	public static final int MAX = 1;

	/**
	 * Cursor position
	 */
	TennisBall racquetP1;

	/**
	 * Constructor
	 * 
	 */
	public TennisGame() {
		super();
		setMinimumSize(new Dimension(1000, 1000));
	}

	public void init() {

		int width = this.getBounds().width;
		int height = this.getBounds().height;
		setDoubleBuffered(true);
		// image = createImage(200, 200);
		// imageGraphics = image.getGraphics();

		balls = new TennisBall[MAX];
		racquetP1 = new TennisBall(width, height, width - 2 * Ball.DIAMETER,
				height / 2, TennisBall.RACQUET_STATE);

		// try {
		// clickSound = getAudioClip(getCodeBase(), "click.au");
		// } catch (Exception e) {
		// }

		// our balls have different start coordinates, increment values
		// (speed, direction) and colors
		for (int i = 0; i < MAX; i++) {
			TennisBall ball = new TennisBall(width, height, (int) (Math
					.random() * (width - Ball.DIAMETER)),
					(int) (Math.random() * (height - Ball.DIAMETER)), 2, 2);
			if (i >= INIT) {
				ball.setState(TennisBall.BALL_STATE);
			}
			balls[i] = ball;
		}
		for (Ball ball1 : balls) {
			getFreePlace(ball1);
		}

		MouseAdapter listener = new MouseAdapter() {
			public void mouseMoved(MouseEvent e) {
				racquetP1.setX(e.getPoint().getX() - Ball.DIAMETER / 2);
				racquetP1.setY(e.getPoint().getY() - Ball.DIAMETER / 2);
				for (int i = 0; i < balls.length; i++) {
					TennisBall ball1 = (TennisBall) balls[i];
					if (ball1.getState() != Ball.DEAD_STATE) {
						if (collide(ball1, racquetP1)) {
							ball1.hit(racquetP1);
							ball1.move();
						}
					}
				}
			}
		};
		addMouseMotionListener(listener);
	}

	public class TessiBallsController extends Thread {

		public void run() {
			while (true) {
				// Thread sleeps for 15 milliseconds here
				try {
					Thread.sleep(10);
				} catch (Exception e) {
				}
				// move our balls around
				for (Ball ball1 : balls) {
					if (ball1.getState() != Ball.DEAD_STATE) {
						ball1.move();
					}
				}
				handleCollision();
				repaint();
			}
		}
	}

	public Thread newBallsController() {
		return new TessiBallsController();
	}

	/***************************************************************************
	 * we iterate through all the balls, checking for collision
	 **************************************************************************/
	protected void handleCollision() {
		for (int i = 0; i < balls.length; i++) {
			TennisBall ball1 = (TennisBall) balls[i];
			if (ball1.getState() != Ball.DEAD_STATE) {
				for (int j = i + 1; j < balls.length; j++) {
					Ball ball2 = balls[j];
					if (ball2.getState() != Ball.DEAD_STATE
							&& collide(ball1, ball2)) {
						ball1.hit(ball2);
						ball2.hit(ball1);
					}
				}
				if (collide(ball1, racquetP1)) {
					ball1.hit(racquetP1);
				}
			}
		}
	}

	/***************************************************************************
	 * 
	 **************************************************************************/

	public void paint(Graphics g) {

		int width = this.getBounds().width;
		int height = this.getBounds().height;

		// draw a gray background and a grid of lines

		g.setColor(Color.gray);
		g.fillRect(0, 0, getBounds().width, getBounds().height);
		g.setColor(Color.lightGray);

		for (int x = 0; x < width; x += 50)
			g.drawLine(x, 0, x, height);

		for (int y = 0; y < height; y += 50)
			g.drawLine(0, y, width, y);

		// if (sound)
		// g.drawString("Click to turn sound OFF", 10, 20);
		// else
		// g.drawString("Click to turn sound ON", 10, 20);

		// paint our balls
		racquetP1.paint(g);

		for (Ball ball1 : balls) {
			if (ball1.getState() != Ball.DEAD_STATE) {
				ball1.paint(g);
			}
		}
		// g.drawImage(Buffer, 0, 0, this);
	}

	public static void main(String[] args) {
		TennisGame game = new TennisGame();
		game.open();
	}

}